package com.cqc.jdk8.Jutil.stream;

import com.cqc.eb.UserEB;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

/**
 * 聚合函数
 */
public class MinMaxCountTest {

    List<UserEB> userEBList = null;
    List<Integer> integerList;

    @Before
    public void before() {
        this.getUserEBList();
        integerList = Arrays.asList(7, 6, 9, 4, 11, 6);
    }


    @Test
    public void f1() {
        userEBList.stream()
                .max(Comparator.comparingInt(UserEB::getAge))
                .ifPresent(System.out::println);

        integerList.stream()
                .max(Comparator.comparingInt(x -> x))
                .ifPresent(System.out::println);
        integerList.stream()
                .max(Integer::compareTo)
                .ifPresent(System.out::println);
        integerList.stream()
                .max(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o1 - o2;
                    }
                })
                .ifPresent(System.out::println);
    }

    @Test
    public void f2() {
        userEBList.stream()
                .min(Comparator.comparingInt(UserEB::getAge))
                .ifPresent(System.out::println);
    }

    @Test
    public void count() {
        long count = userEBList.stream()
                .filter(user -> user.getAge() < 25)
                .count();
        System.out.println(count);
    }


    private void getUserEBList() {
        userEBList = new ArrayList<>();
        userEBList.add(new UserEB("张三", 20));
        userEBList.add(new UserEB("李四", 21));
        userEBList.add(new UserEB("王五", 22));
        userEBList.add(new UserEB("刘诗诗", 30));
        userEBList.add(new UserEB("刘亦菲", 31));
        userEBList.add(new UserEB("古力娜扎", 28));
        userEBList.add(new UserEB("古力娜扎", 26));
    }

}
